﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using System.Xml;
using System.Collections;

public class FightLogic : MonoBehaviour
{
    private ModelLocator model;
    /// <summary>
    /// 存放战斗中的对象
    /// </summary>
    private List<GameObject> objList = new List<GameObject>();

    /// <summary>
    /// 战报信息
    /// </summary>
    private AttackVo attackVo;

    /// <summary>
    /// 战斗初始化基本信息
    /// </summary>
    private List<FightInfoVo> fightInfoList;

    /// <summary>
    /// 战斗回合信息
    /// </summary>
    private List<BoutVo> boutVos;

    /// <summary>
    /// 战斗一个回合的流程信息
    /// </summary>
    private List<FlowVo> _flowVos;

    /// <summary>
    /// 战斗场景
    /// </summary>
    private GameObject fightScene = new GameObject();

    public FightLogic() { }

    void Awake()
    {
        model = ModelLocator.getInstance();
        model.addEventListener(1, receiveFightReport);
    }

    /// <summary>
    /// 接收战报
    /// </summary>
    private void receiveFightReport(object o, DispatchEventArgs e)
    {
        ByteArrayEx fightByteArr = new ByteArrayEx((byte[])e.data[0]);
        string fightStr = fightByteArr.readMultiByteEx();

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(fightStr);

        attackVo = new AttackVo(xmlDoc);
        fightInfoList = attackVo.fightVos;
        boutVos = attackVo.boutVos;

        LoadSkill();//可改协同//

        initFightScene();

        startFight(attackVo);
    }

    /// <summary>
    /// 加载技能资源到制定位置，对应指定名称战斗中使用
    /// </summary>
    private void LoadSkill()
    {
        List<string> skill = new List<string>();//存放这场战斗的所有技能名//
        foreach (FightInfoVo fightInfo in fightInfoList)
        {
            skill.Add(fightInfo.skill);
        }

        foreach (BoutVo boutVo in boutVos)
        {
            foreach (FlowVo flowVo in boutVo.flowVos)
            {
                if (skill.IndexOf(flowVo.skill) == -1) skill.Add(flowVo.skill);
            }
        }

        //..加载资源,初始化到场景..//


    }

    /// <summary>
    /// 初始化战斗场景
    /// </summary>
    private void initFightScene()
    {
        for (int i = 0; i < fightInfoList.Count; i++)
        {
            //objList.Add(xxx);//初始化FightInfo信息,增加到objList//
            //GameObject.tag=fightInfoList[i].pos//
            //GameObject.transform.parent = fightScene.transform//

            //xxx.<AttackPlayer>.fightInfoVo = fightInfoList[i];//
        }

    }

    /// <summary>
    /// 开始战斗
    /// </summary>
    public void startFight(AttackVo attackVo)
    {
        startBout();
    }

    /// <summary>
    /// 开始新的回合
    /// </summary>
    private void startBout()
    {
        BoutVo boutVo = boutVos[0];
        _flowVos = boutVo.flowVos;
        boutVos.RemoveAt(0);

        string strCount = boutVo.count.ToString();//回合数//
        //显示"第xx回合"//
        //showBountNum(strCount);//

        resetPlayerInfo(boutVo.boutInfoVos);

        onUpdateAttackDepth();

        fightControl();
    }

    /// <summary>
    /// 初始化每回合的战斗对象基本信息
    /// </summary>
    private void resetPlayerInfo(List<BoutInfo> boutInfos)
    {
        foreach (BoutInfo boutInfo in boutInfos)
        {
            GameObject fightObj = getAttackPlayerFromPos(boutInfo.pos);//每个战斗对象的//

            if (fightObj != null && fightObj.activeSelf)
            {
                //xx脚本中含有下列方法实现//
                //fightObj.GetComponent<"AttackPlayer">.resetHP(boutInfo.hp, boutInfo.maxhp);//重置血量//
                //fightObj.GetComponent<"AttackPlayer">.resetMP(boutInfo.mp);//重置气势//
                //fightObj.GetComponent<"AttackPlayer">.showBuffer( boutInfo.buffer );//显示此回合buff效果//
            }

        }
    }

    /// <summary>
    /// 深度排序
    /// </summary>
    private void onUpdateAttackDepth()
    {
        GameObject[] arrObj = objList.ToArray();
        arrObj = BubbleSort(arrObj);

        float indexStartVal = 0;

        foreach (GameObject obj in arrObj)
        {
            Vector3 pos = obj.transform.localPosition;
            obj.transform.localPosition = new Vector3(pos.x, pos.y, indexStartVal);
            indexStartVal += 0.001f;
        }

    }
    private GameObject[] BubbleSort(GameObject[] list)
    {
        int i;
        GameObject temp;
        for (int j = 0; j < list.Length; j++)
        {
            for (i = list.Length - 1; i > j; i--)
            {
                if (list[j].transform.localPosition.y > list[i].transform.localPosition.y)
                {
                    temp = list[j];
                    list[j] = list[i];
                    list[i] = temp;
                }
            }
        }
        return list;
    }

    /// <summary>
    /// 战斗流程控制
    /// </summary>
    private void fightControl()
    {
        if (0 >= _flowVos.Count)
        {
            //说明要新回合了//
            if (0 == attackVo.boutVos.Count)
            {
                fightOver();//战斗结束//
            }
            else
            {
                startBout();
            }
        }
        else
        {
            startFlow();
        }
    }

    /// <summary>
    /// 开始单个回合的战斗流程
    /// </summary>
    private void startFlow()
    {
        FlowVo flow = _flowVos[0];
        _flowVos.RemoveAt(0);

        //此流程中无伤害数据则跳过//
        if (flow.damageVos.Count == 0)
        {
            fightControl();
            return;
        }

        AttackActionVo attackActionVo = new AttackActionVo(flow);//攻击行为数据//

        //施法者//
        GameObject fPlayer = getAttackPlayerFromPos(flow.attackPos);
        //目标者数组//
        List<GameObject> tPlayersArr = new List<GameObject>();
        tPlayersArr.Add(getAttackPlayerFromPos(flow.targetPos));

        for (int i = 0; i < flow.damageVos.Count; i++)
        {
            tPlayersArr.Add(getAttackPlayerFromPos(flow.damageVos[i].pos));
        }

        if (attackActionVo.skillAttackType == 5)//区分技能攻击还是普通攻击(技能需要展示技能名称)//
        {
            //showSkillEffectName(attackActionVo.skill);//显示"xxx技能"//
        }

        //根据不同技能类型实现对应显示逻辑//
        //PlayerFightAction.OnNormalAttack;PlayerFightAction.OnSkillAttack;PlayerFightAction.OnGroupAttack;//
        switch(attackActionVo.flag)
        {
            case "111":
                break;
            case "112":
                break;
            case "121":
                break;
            case "122":
                break;
            case "211":
                break;
            case "212":
                break;
            case "221":
                break;
            case "222":
                break;
        }


    }

    /// <summary>
    /// 战斗结束
    /// </summary>
    private void fightOver()
    {
        //model.dispacthEvent();//派发战斗结束,接收后处理//
    }



    /// <summary>
    /// 按位置取战斗对象
    /// </summary>
    private GameObject getAttackPlayerFromPos(int pos)
    {
        foreach (GameObject obj in objList)
        {
            if (obj.tag == pos.ToString())
            {
                return obj;
            }
        }
        return null;
    }




}